import Vue from 'vue'
import axios  from 'axios'
import { Toast } from 'vant';
Vue.use(Toast);
let toast = null
// 创建 axios 实例对象
const instance = axios.create({
    baseURL:'http://shop.bufantec.com',
    timeout:5000
})

// 请求拦截器
// Add a request interceptor
instance.interceptors.request.use(function (config) {
    // 显示进度条
    toast = Toast.loading({
        message: '加载中...',
        forbidClick: true,
        duration:0
      })
    // 如果是get方式请求
    if(config.method.toLowerCase() === 'get'){
        config.params = config.data
    }
    // console.log('config=>',config);
    return config;
  }, function (error) {
    // Do something with request error
    return Promise.reject(error);
  });

// axios 响应拦截器
instance.interceptors.response.use(function (response) {
    // console.log('response=>',response);
    // 关闭进度条
    toast.clear()
    return response.data;
  }, function (error) {
    // Any status codes that falls outside the range of 2xx cause this function to trigger
    // Do something with response error
    return Promise.reject(error);
  });

export default instance